class TradeDetector:
    def __init__(self, threshold_percentage=2):
        self.threshold_percentage = threshold_percentage

        self.minute_data = {'002134.SZ': {'20230718': [11.0, 11.5, 11.3, 10.9, 10.3, 10.1, 11.0],
                                          '20230719': [11.0, 11.5, 11.3, 10.9, 10.3, 10.1, 10.0]},
                            '002905.SZ': {'20230718': [11.0, 11.5, 11.3, 10.9, 10.3, 10.1, 11.0],
                                          '20230719': [11.0, 11.5, 11.3, 10.9, 10.3, 10.1, 10.0]}
                            }

    def detection(self, stock_pool, current_date):
        alarm_stocks = {}
        for stock_code, stock_info in stock_pool.items():
            state_dt = stock_info.get('interval')[-1][1]
            cost_price = stock_info.get('cost_price')

            try:
                # 检测最新的分钟数据
                current_price = self.minute_data[stock_code][current_date][-1]
                close_percentage = (current_price - cost_price) / cost_price * 100

                # 检查差值百分比是否足够小于阈值范围
                if 0 <= close_percentage <= self.threshold_percentage:
                    alarm_stocks[stock_code] = {'close_percentage': close_percentage,
                                                'current_price': current_price}
            except Exception as e:
                print(e)
                continue

        return alarm_stocks

